import React from 'react';

interface Props {

}

interface State {
    component ?: any;
}



// 告诫组件得用泛型；
// export default function asyncComponent<T>(importComponent: React.ComponentType<T>): React.ComponentType<T> {
    export default function asyncComponent(importComponent: any): any {
    class AsyncComponent extends React.Component<Props, State> {
        constructor(props: Props) {
            super(props)
            this.state = {
                component: null,
            }
        }

        async componentDidMount() {
            if (this.hadComponent()) {
                return
            }
            const { default: component } = await importComponent();

            this.setState({ component });
        }

        //组件被销毁之前重写setState方法 不对状态做任何改变
        componentWillUnmount() {
            this.setState = (state, callback) => {
                return
            };
        }

        hadComponent = ():any => {
            // return this.state.component !== null
        }

        render() {
            const C = this.state.component;

            return C ? <C {...this.props} /> : null;
        }

    }

    return AsyncComponent;
}